package top.chenfu.blog.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import top.chenfu.blog.dao.PvMapper;
import top.chenfu.blog.entity.Pv;
import top.chenfu.blog.entity.User;
import top.chenfu.blog.service.PvService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther chenfu
 * @2019/11/1 12:46
 * @Desc
 */
@Service
public class PvServiceImpl implements PvService {

    @Autowired
    private PvMapper pvMapper;

    @Override
    public void pvStatisticsPerDay() {
//        首先查出每个用户的当前访问总量
        List<Map<String, Object>> articleSattistics = pvMapper.articlePageViewStatistic();
//        统计每个人的历史访问总量【不包含今天的访问量】
        List<Map<String, Object>> pvStatistics = pvMapper.pvStatistic();

        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterDay = new Date(instance.getTimeInMillis());

//        第一次跑定时任务
        if (CollectionUtils.isEmpty(pvStatistics)) {
            firstPvStatistic(articleSattistics, yesterDay);
        } else {
            pvStatistic(articleSattistics, pvStatistics, yesterDay);
        }
    }

    /**
     * 获取最近七天的日期
     *
     * @return
     */
    @Override
    public List<String> getCategories() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return pvMapper.selectDateByUid(principal.getId());
    }

    @Override
    public List<Integer> getPvStatistics() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return pvMapper.selectPvStatistics(principal.getId());
    }

    private void pvStatistic(List<Map<String, Object>> articleSattistics, List<Map<String, Object>> pvStatistics, Date today) {
        for (int index = 0; index < articleSattistics.size(); index++) {
//                用当天的总访问量减去以前的访问量，得到当前的访问量
            if (articleSattistics.get(index).get("uid").equals(pvStatistics.get(index).get("uid"))) {
                long currentPv = Long.parseLong(articleSattistics.get(index).get("totalPv").toString()) - Long.parseLong(pvStatistics.get(index).get("pv").toString());
                Pv pv = new Pv();
                pv.setCountDate(today);
                if (currentPv > 0) {
                    pv.setPv(currentPv);
                } else {
                    pv.setPv(0L);
                }
                pv.setUid(Long.parseLong(articleSattistics.get(index).get("uid").toString()));
                pvMapper.insert(pv);
            }
        }
    }

    private void firstPvStatistic(List<Map<String, Object>> articleSattistics, Date today) {
        for (Map<String, Object> articleSattistic : articleSattistics) {
            Pv pv = new Pv();
            pv.setCountDate(today);
            pv.setPv(Long.parseLong(articleSattistic.get("totalPv").toString()));
            pv.setUid(Long.parseLong(articleSattistic.get("uid").toString()));
            pvMapper.insert(pv);
        }
    }

}
